package com.powernode.controller;

import com.powernode.framework.Result;
import com.powernode.framework.ResultBean;
import com.powernode.framework.ResultUtil;
import com.powernode.pojo.DormitoryAdmin;
import com.powernode.pojo.FloorAdmin;
import com.powernode.pojo.IsolatedMan;
import com.powernode.pojo.TowerAdmin;
import com.powernode.service.DormitoryAdminService;
import com.powernode.service.FloorAdminService;
import com.powernode.service.IsolatedManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycc
 * @Date: 2023/2/14 12:16
 */
@RestController
@RequestMapping("/api/isolatedMan")
public class IsolatedManController {
    @Autowired
    IsolatedManService isolatedManService;//隔离者
    @Autowired
    DormitoryAdminService dormitoryAdminService;
    @Autowired
    FloorAdminService floorAdminService;

    @PostMapping("/save")
    public Result<String> modify(@RequestBody IsolatedMan isolatedMan,HttpServletRequest request) {
        FloorAdmin floorAdmin1 = (FloorAdmin) request.getSession().getAttribute("floorAdmin");
        if(floorAdmin1.getFloorSite().equals(isolatedMan.getSite().substring(0,5))){ //新增的隔离者，或者将本楼内的隔离者修改，也只能新增或修改为是本楼内人员
            if (isolatedMan.getIsolatedBeginTime().getTime()<isolatedMan.getHaveIllTime().getTime()){
                return ResultUtil.fail("开始隔离日期，应在发现病情之后！");
            }
            if (isolatedMan.getRole().equals("寝室长")){ //如果是寝室长，判断一下该寝室是否已有寝室长
                DormitoryAdmin dormitoryAdmin = dormitoryAdminService.selectBySite(isolatedMan.getSite());
                if (dormitoryAdmin!=null && !dormitoryAdmin.getStudentNumber().equals(isolatedMan.getStudentNumber())){//如果原来这个寝室已经有寝室长，并且不是这个
                    return ResultUtil.fail("此寝室已经有寝室长，且非此人，请修改角色！");
                }
            }
            isolatedManService.modify(isolatedMan);
            return ResultUtil.ok();
        }else {
            return ResultUtil.fail("添加/修改隔离者,只能在负责的楼内进行，请修改住处！");
        }

    }

    @PostMapping("/remove")
    public Result<String> remove(@RequestBody Integer[] ids) {
        System.out.println(ids[0]);
        isolatedManService.removes(ids);
        return ResultUtil.ok();
    }

    @GetMapping("/list")
    public ResultBean<List<IsolatedMan>> getPageResult(
            HttpServletRequest request,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String site,
            @RequestParam(required = false) String role,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {

        FloorAdmin floorAdmin = (FloorAdmin)request.getSession().getAttribute("floorAdmin");
        if ( site == null || site.equals("") ){
            site = floorAdmin.getFloorSite();
        }else {
            if(!site.substring(0,5).equals(floorAdmin.getFloorSite())){
                return new ResultBean<>(0, "不是此楼层的寝室号", 0, null);
            }
        }

        Map<String, Object> param = new HashMap<>(16);
        int offset = (page - 1) * limit;
        int rows = limit;
        param.put("name", name);
        param.put("site", site);
        param.put("role", role);
        param.put("offset", offset);
        param.put("rows", rows);
        int totalRows = isolatedManService.count(param);
        List<IsolatedMan> entities = isolatedManService.getPageResult(param);
        return new ResultBean<>(0, "查询成功", totalRows, entities);
    }
}